#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pygtk
pygtk.require('2.0')
import gtk
import gtkmozembed
import os

class Dict:
 	def app_close(self, widget, event, data=None):
		gtk.main_quit()
		return False
	def enter_callback(self, widget, word_entry):
		search_word = word_entry.get_text()
		if len(search_word) == 0:
			self.moz.load_url("file://"+os.path.dirname(os.path.abspath(__file__))+'/home.html')
		else:
			self.moz.load_url("http://dict.cn/mini.php?q=" + search_word)
	def go_forward(self, widget, data=None):
		self.moz.go_forward()
	def go_back(self, widget, data=None):
		self.moz.go_back()
	def clear_entry(self, widget, word_entry):
		word_entry.set_text('')
		self.enter_callback(widget,word_entry)

	def __init__(self):
		# Main Window Initialization
		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		self.window.connect('delete_event', self.app_close)
		self.window.set_title("Live Dict")
		self.window.set_border_width(5)
	        self.window.set_default_size(500, 400)

		# Set Layout for GUI
		vbox11 = gtk.VBox(False, 3)
		self.window.add(vbox11)
		vbox11.show()
		hbox21 = gtk.HBox(False, 0)
		hbox22 = gtk.HBox(False, 0)
		vbox11.pack_start(hbox21, False, False, 0)
		vbox11.pack_start(hbox22, True, True, 0)
		hbox21.show()
		hbox22.show()

		# Clear Button Initialization
		clear_button = gtk.Button(stock=gtk.STOCK_CLEAR)
		clear_button.set_relief(gtk.RELIEF_NONE)
		hbox21.pack_start(clear_button, False, False, 0)
		clear_button.show()
		# Text Entry
		word_entry = gtk.Entry()
		word_entry.connect("activate",self.enter_callback,word_entry)
   		clear_button.connect("clicked", self.clear_entry, word_entry)
		hbox21.pack_start(word_entry, True, True, 0)
		word_entry.show()
		# Forward Button
		forward_button = gtk.Button()
		forward_button.connect("clicked",self.go_forward)
		forward_button.set_image(gtk.image_new_from_stock(gtk.STOCK_GO_FORWARD,gtk.ICON_SIZE_BUTTON))
		forward_button.set_relief(gtk.RELIEF_NONE)
		hbox21.pack_end(forward_button, False, False, 0)
		forward_button.show()
		# Back Button
 		back_button = gtk.Button()
		back_button.connect("clicked",self.go_back)
		back_button.set_image(gtk.image_new_from_stock(gtk.STOCK_GO_BACK,gtk.ICON_SIZE_BUTTON))
		back_button.set_relief(gtk.RELIEF_NONE)
		hbox21.pack_end(back_button, False, False, 0)
		back_button.show()
		# Search Button
		search_button = gtk.Button()
		search_button.connect('clicked', self.enter_callback,word_entry)
		search_button.set_image(gtk.image_new_from_stock(gtk.STOCK_FIND,gtk.ICON_SIZE_BUTTON))
		search_button.set_relief(gtk.RELIEF_NONE)
		hbox21.pack_end(search_button, False, False, 5)
		search_button.show()
		# Mozembed
                self.moz = gtkmozembed.MozEmbed()
		self.moz.load_url("file://"+os.path.dirname(os.path.abspath(__file__))+'/home.html')
                hbox22.pack_start(self.moz, True, True, 0)
		self.moz.show()

		self.window.show()
def main():
	dict = Dict()
	gtk.main()

if __name__ == '__main__':
	main()


